#include "Driver_Lora.h"
#include "myuart.h"


//配置模式
void LoRa_Mode(GPIO_PinState M0,GPIO_PinState M1)
{
    HAL_GPIO_WritePin(LoRa_M0_GPIO_Port, LoRa_M0_Pin, M0);
    HAL_GPIO_WritePin(LoRa_M1_GPIO_Port, LoRa_M1_Pin, M1);
    HAL_Delay(20);   //太快的话LoRa反映不过来
}


//点对点发送
void LoRa_Transmit_PtoP(uint8_t addr_h, uint8_t addr_l, uint8_t channel, uint8_t* pData, uint16_t Size, uint32_t timeout)
{
    /* LoRa配置模式 */
    LoRa_Mode(0,0);
    
    /* 配置波特率 */
    MyUART1_Init(LORA_BAUD);
    
    /* 发送目标地址 */
    HAL_UART_Transmit(&huart1, &addr_h, 1, timeout);
    HAL_UART_Transmit(&huart1, &addr_l, 1, timeout);
     /* 发送目标信道 */
    HAL_UART_Transmit(&huart1, &channel, 1, timeout);
    /* 发送数据 */
    HAL_UART_Transmit(&huart1, pData, Size, timeout);
}


//初始化LoRa
void Lora_Init(uint8_t satrt_addr, uint8_t size, uint8_t addr_h, uint8_t addr_l, uint8_t netid, uint8_t reg0, uint8_t reg1, uint8_t reg2, uint8_t reg3)
{
    /* LoRa配置模式 */
    LoRa_Mode(0,1);
    /* 配置LoRa */
    uint8_t us_TxLoraInit[10] = {0XC0, satrt_addr, size, addr_h, addr_l, netid, reg0, reg1, reg2, reg3};
    MyUART1_Init(9600);          //配置时波特率固定为9600 8 N 1
    HAL_UART_Transmit(&huart1, us_TxLoraInit, sizeof(us_TxLoraInit), 200) ;     //发送给LoRa
    
   /* 判断返回值 */
    uint8_t us_RxLoraInit[10];  
    HAL_UART_Receive(&huart1, us_RxLoraInit, 10, 200);
    us_TxLoraInit[0] = 0XC1;
    int Val = (memcmp(us_TxLoraInit, us_RxLoraInit, sizeof(us_TxLoraInit)) == 0);
    if (Val) {
       /* 设置成功 */    
    }else{
       return;
    }
}

